package com.sixeco.apigetway.common;

import java.net.SocketAddress;
import java.nio.channels.spi.SelectorProvider;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.DefaultSelectStrategyFactory;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.AsciiString;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.RejectedExecutionHandlers;
import io.netty.util.concurrent.ScheduledFuture;

/**
 * 
 * @author tianzhenjiu
 *
 */
public class CommonVar {

	
	
	/**
	 * http请求相关
	 * @author tianzhenjiu
	 *
	 */
	public static class HttpChunk{
		
		
		
		/**
		 * 支持最大的字节，防止ddos
		 */
		public final static int maxInitialLineLength=4096;
		public final static int maxHeaderSize=81920;
		public final static int maxChunkSize=81920;
		
		/**
		 * 常用的contenttype
		 */
		public static final AsciiString CONTENT_TYPE = AsciiString.cached("Content-Type");
		public static final AsciiString CONTENT_LENGTH = AsciiString.cached("Content-Length");
		public static final AsciiString CONNECTION = AsciiString.cached("Connection");
		public static final AsciiString KEEP_ALIVE = AsciiString.cached("keep-alive");
		
		
		/**
		 * 常用错误的content
		 */
		public static final byte[] unSouppert = "Unsouppert request Method".getBytes();
		
		public static final byte[] serverError = "Internal Server error".getBytes();
		
		public static final byte[] proxyErrr = "PROXYserver is ERROR".getBytes();
		
		/**
		 * SSL相关
		 */
		public static String SSL_PASSWORD="123456";
		public static String SSL_JKSPATH;
		public static boolean SSL_ISENABLE=false;
		
		
		/**
		 * 重写次数
		 */
		public static int reWriteTimes=3;
		
	}
	
	
	/**
	 * 代理相关
	 * @author tianzhenjiu
	 *
	 */
	public static class Proxy{
		
		public static int initchannelCount=2;
		
		public static int threadCount=4;
		
		public static NioEventLoopGroup httpgroup = new NioEventLoopGroup(
				threadCount, 
				(Executor)null,
				MyEventLoopChooserFactory.INSTANCE,
				SelectorProvider.provider(),
				DefaultSelectStrategyFactory.INSTANCE,RejectedExecutionHandlers.reject());
		
		/**
		 * the ab channel bound to  bc channel 's attr
		 */
		public static final AttributeKey<Channel> bcChannelAttr = AttributeKey.valueOf("bcChannelAttr");
		
		
		
		/**
		 * the bc channel  bound to  ab  channels 's attr
		 */
		public static final AttributeKey<Channel> abChannelAttr = AttributeKey.valueOf("abChannelAttr");
	
		/**
		 * the ab channel last requst time
		 */
		public static final AttributeKey<Long> lastRequestTime = AttributeKey.valueOf("lastRequestTime");
		
		
		/**
		 * the ab channel  last request uri
		 */
		public static final AttributeKey<String> lastRequestUri = AttributeKey.valueOf("lastRequestUri");
		/**
		 * the ab channel request times
		 */
		public static final AttributeKey<AtomicInteger> requstCount = AttributeKey.valueOf("requstCount");
	
		
		/**
		 * 报文收到标识
		 */
		public static final AttributeKey<ScheduledFuture<?>> reWriteFuture = AttributeKey.valueOf("reWriteFuture");
		
		/**
		 * 代理socket数组
		 */
		public static  List<SocketAddress> proxySockets;
		
		
	}
	
	
	/**
	 * 创建一个无效请求的报文
	 * @return
	 */
	public static HttpResponse createBadRequest(String msg) {
		
		
		ByteBuf content=null;
		
		if(msg==null||msg.length()==0) {
			content=Unpooled.wrappedBuffer(CommonVar.HttpChunk.unSouppert);
		}else {
			content=Unpooled.wrappedBuffer(msg.getBytes());
		}
		
		FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
				HttpResponseStatus.BAD_REQUEST,
				content);
		
		return response;
	}
	
	
	/**
	 * 创建一个服务错误的报文
	 * @return
	 */
	public static HttpResponse createServerErrorRequest() {
		
		FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
				HttpResponseStatus.INTERNAL_SERVER_ERROR,
				Unpooled.wrappedBuffer(CommonVar.HttpChunk.serverError));
		
		return response;
	}


	
}
